package com.jeffers.videorecord;

import android.media.session.MediaController;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import java.io.File;
import java.util.Arrays;
import java.util.Comparator;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity{

	@Bind(R.id.video_list)
	ListView mVideoListView;
	@Bind(R.id.video_view)
	MediaController mMediaController;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // test git
		ButterKnife.bind(this);

		getVideoFile();
    }

	@Override
	protected void onDestroy() {
		super.onDestroy();
		ButterKnife.unbind(this);
	}

	@OnClick(R.id.native_record_btn)
	public void nativeBtnOnClick(Button nativeBtn){
		FragmentContainerActivity.startActivity(this, NativeVideoRecordFragment.class, null);
	}

	@OnClick(R.id.other_record_btn)
	public void otherBtnOnClick(Button otherBtn){

	}

	private void getVideoFile(){
		String cacheDir = StorageUtils.getCacheDirectory(this, "");
		if(!TextUtils.isEmpty(cacheDir)) {
			File dirFile = new File(cacheDir);
			File[] videoFiles = dirFile.listFiles();
			Arrays.sort(videoFiles, new Comparator<File>() {
				@Override
				public int compare(File lhs, File rhs) {
					if (lhs.lastModified() == rhs.lastModified()) {
						return 0;
					} else if (lhs.lastModified() < rhs.lastModified()) {
						return 1;
					}
					return -1;
				}
			});
			ArrayAdapter<File> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, videoFiles);
			mVideoListView.setAdapter(adapter);
		}
	}

}
